package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 防止跨域
@RestController
// 请求网址路径
@RequestMapping("/user/shoppingCart")
// 日志
@Slf4j
// 接口文档
@Api(tags = "购物车接口")
public class ShoppingCarController {

// 注入购物车业务类
    @Autowired
    private ShoppingCartService shoppingCartService;
//     添加购物车
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result addC(@RequestBody ShoppingCartDTO shoppingCartDTO){
        //输出日志
        log.info("添加至购物车购物信息:{}",shoppingCartDTO);
        //操作业务类
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        //

        //
        return Result.success();
    }

    /**
     * 查看购物车
     * */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list(){
        List<ShoppingCart> lists = shoppingCartService.showShoppingCart();
        return Result.success(lists);
    }

    /**
     * 删除单个商品
     *
     * */
    @PostMapping("/sub")
    @ApiOperation("删除单个商品")
    public Result deleteById(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除单个商品:{}",shoppingCartDTO);
        shoppingCartService.deleteById(shoppingCartDTO);
        return Result.success();
    }
    /**
     * 清空购物车
     *
     * */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clean(){
        shoppingCartService.clean();
        return Result.success();
    }
}
